package com.qinxueit.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qinxueit.admin.api.ParseHtml;
import com.qinxueit.admin.api.SysConfig;
import com.qinxueit.admin.entity.Article;
import com.qinxueit.admin.entity.Chapter;
import com.qinxueit.admin.entity.Sys;
import com.qinxueit.admin.service.ArticleService;
import com.qinxueit.admin.service.ChapterService;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class TaskController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ChapterService chapterService;

    @GetMapping("/updateArticle")
    public void updateArticle() throws Exception {
        Sys sysConfig = SysConfig.getSysVal();
        String url = sysConfig.getSysValue();

        for (int i = 0; i < 20; i++) {
            for (int j = 1; j < 10000; j++) {
                String parseUrl = url + "/" + i + "_" + j + "/";
                System.out.println(parseUrl);
                Document document = ParseHtml.getHtmlContent(parseUrl);
                //System.out.println(document);

                if (document != null) {
                    Article article = ParseHtml.getArticle(document);
                    System.out.println(article);

                    if (article.getAuthor() != null || !article.getAuthor().isEmpty()) {
                        boolean b1 = articleService.save(article);

                        List<Chapter> chapterList = ParseHtml.getChapter(document, article);
                        boolean b2 = chapterService.saveBatch(chapterList);
                    }
                }
                Thread.sleep(2000);
            }
        }
    }

    @GetMapping("/test")
    public void test() throws IOException, InterruptedException {
        List<Article> articleList = articleService.list(new QueryWrapper<Article>().select("article_id", "url_status"));
        for (Article article : articleList) {
            List<Chapter> chapterLists = chapterService.list(new QueryWrapper<Chapter>().isNotNull("source_url").eq("url_status", article.getUrlStatus()));
            //System.out.println(articleLists);
            ArrayList<Chapter> chapterArrayList = new ArrayList<>();
            for (Chapter chapter : chapterLists) {
                Chapter newChapter = ParseHtml.getChapterList(chapter);
                //System.out.println("newChapter对象内容是:" + newChapter);
                if (newChapter != null) {
                    chapterArrayList.add(newChapter);
                }

                Thread.sleep(2000);
            }

            if (!chapterArrayList.isEmpty()) {
                System.out.println(chapterArrayList);
                boolean b = chapterService.saveOrUpdateBatch(chapterArrayList);
                if (b == true) {
                    article.setBookStatus(1);
                    articleService.save(article);
                }

            }
        }
    }
}
